package com.lezijie.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lezijie.seckill.exception.GlobalException;
import com.lezijie.seckill.mapper.UserMapper;
import com.lezijie.seckill.pojo.User;
import com.lezijie.seckill.service.IUserService;
import com.lezijie.seckill.utils.CookieUtil;
import com.lezijie.seckill.utils.MD5Util;
import com.lezijie.seckill.utils.UUIDUtil;
import com.lezijie.seckill.vo.LoginVO;
import com.lezijie.seckill.vo.RespBean;
import com.lezijie.seckill.vo.RespBeanEnum;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * UserService实现类
 * @author SunZhiPeng
 * @since 2021-10-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 登录功能
     * @param loginVO 登录参数
     * @param request
     * @param response
     * @return RespBean对象，登录成功或者失败参数
     */
    @Override
    public RespBean doLogin(LoginVO loginVO, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        // if (!(StringUtils.hasLength(mobile) || StringUtils.hasLength(password))) {
        //     return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        // }
        // if (!ValidatorUtil.isMobile(mobile)) {
        //     return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        // }
        User user = baseMapper.selectById(mobile);
        if (null == user) {
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
            // return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        if (!MD5Util.formPassToDbPass(password,user.getSlat()).equals(user.getPassword())) {
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
            // return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        // 生成Cookie
        String ticket = UUIDUtil.uuid();
        redisTemplate.opsForValue().set("user:" + ticket,user);
        // request.getSession().setAttribute(ticket,user);
        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return RespBean.success();
    }

    /**
     * 根据Cookie获取用户
     * @param userTicket 用户ticket
     * @param request 四大作用域 request
     * @param response 响应：response
     * @return 用户
     */
    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (!StringUtils.hasLength(userTicket)) {
            return null;
        }
        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);
        // 如果这个用户不是空，那么重新设置一下Cookie，害怕Cookie过时
        if (user != null) {
            CookieUtil.setCookie(request,response,"userTicket", userTicket);
        }
        return user;
    }
}
